<template>
    <basic-container>
        <avue-crud :option="option"
                   :data="data"
                   ref="crud"
                   v-model="form"
                   :table-loading="loading"
                   :page="page"
                   @refresh-change="refreshChange"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @current-change="currentChange"
                   @size-change="sizeChange"
                   @on-load="onLoad">
            <template slot-scope="scope" slot="menuLeft">
                <el-button type="primary" size="mini" @click="handleAdd()" v-has="'/mall-cloud-base/navigation/addItem'">新增
                </el-button>
            </template>
            <template slot-scope="{row,type,size}" slot="menu">
                <el-button :type="type" :size="size" icon="el-icon-edit" @click="getEdit(row)"
                           v-has="'/mall-cloud-base/navigation/editItem'">编辑
                </el-button>
                <el-button :type="type" :size="size" icon="el-icon-view" @click="getView(row)"
                           v-has="'/mall-cloud-base/navigation/detail'">查看
                </el-button>
            </template>
        </avue-crud>

        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" @close="closeDrawer" width="90%">
            <avue-form :disabled=isView ref="forms" v-model="form" :option="newOption" @submit="submit">
                <template slot-scope="" slot="menuForm">
                    <el-button @click="closeDrawer" v-if="!isView">取消</el-button>
                    <el-button @click="closeDrawer" v-else> 关闭</el-button>
                </template>
            </avue-form>
        </el-dialog>
    </basic-container>
</template>
<script>
    import { onlyDateFormat } from '@/util/date';
    import {validateNumEnglish} from '@/util/util.js'
    import {
        getList,
        update,
        add,
        getDetail
    } from "@/api/base/operations/paymentWay";
    var Dict = JSON.parse(localStorage.getItem('mall-dictionary')).content;
export default {
    data() {
        return {
            dialogTitle:'',
            dialogVisible:false,
            isView:false, // 是否是查看
            loading:true,
            option: {
                align:'center',
                slot: true,
                size: 'mini',
                addBtn: false,
                delBtn:false,
                editBtn: false,
                tip: false,
                border: true,
                index: false,
                viewBtn: false,
                rowKey: 'id',
                height: 'auto',
                dialogType: 'drawer',
                menuWidth: 300,
                column: [
                    {
                        label: "渠道名字",
                        prop: "channelName",
                        fixed: true,
                        maxlength:6,
                    },
                    {
                        label: "添加时间",
                        prop: "addTime",
                        // width: 100,
                    },
                ]
            },
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0
            },
            data:[],
            form:{},
            query: {},
            newOption: {
                submitBtn: this.isView?false:true,
                emptyBtn: false,
                border: true,
                height: 'auto',
                labelWidth:200,
                column: [
                    {
                        label: "渠道名字",
                        prop: "channelName",
                        fixed: true,
                        maxlength:6,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message:'请输入渠道名字'
                        }]
                    },
                    {
                        label: "添加时间",
                        prop: "addTime",
                        disabled: true,
                    },
                    {
                        label:'招行门店openId',
                        prop:'zhPayOpenId',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message:'请输入招行门店openId'
                        },{
                            trigger:'blur',
                            validator: validateNumEnglish
                        }]
                    },
                    {
                        label:'招行门店openKey',
                        prop:'zhPayOpenKey',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message:'请输入招行门店openId'
                        },{
                            trigger:'blur',
                            validator: validateNumEnglish
                        }]
                    },
                    {
                        label:'招行主管密码',
                        prop:'zhPayShopPass',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message:'请输入招行主管密码'
                        }]
                    },
                    {
                        label:'工行商户公匙路径',
                        prop:'ghPayMerCrtPath',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行商户公匙路径'
                        }]
                    },
                    {
                        label:'工行商户私匙路径',
                        prop:'ghPayMerKeyPath',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行商户公匙路径'
                        }]
                    },
                    {
                        label:'工行商户证书密码',
                        prop:'ghPayMerKeyPassword',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行商户公匙路径'
                        }]
                    },
                    {
                        label:'工行商户代号',
                        prop:'ghPayMerId',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行商户公匙路径'
                        },{
                            trigger:'blur',
                            validator: validateNumEnglish
                        }]
                    },
                    {
                        label:'工行商户账号',
                        prop:'ghPayMerAcct',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行商户公匙路径'
                        },{
                            trigger:'blur',
                            validator: validateNumEnglish
                        }]
                    },
                    {
                        label:'工行shopAppId',
                        prop:'ghPayShopAppId',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行商户公匙路径'
                        },
                            {
                            trigger:'blur',
                            validator: validateNumEnglish,
                        }]
                    },{
                        label:'工行分配的AppId',
                        prop:'ghPayAppId',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行分配的AppId'
                        }]
                    },{
                        label:'工行商户私匙',
                        prop:'ghPayMyPrivateKey',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行商户私匙'
                        }]
                    },{
                        label:'招行支付标签',
                        prop:'zhPayPmtTag',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入招行支付标签'
                        }]
                    },{
                        label:'工行支付channel_merid',
                        prop:'ghPayChannelMerid',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行支付channel_merid'
                        }]
                    },{
                        label:'工行银行公钥路径',
                        prop:'ghPayBankCrtPath',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行银行公钥路径'
                        }]
                    },{
                        label:'工行网关公匙',
                        prop:'ghPayApigwPublicKey',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行网关公匙'
                        }]
                    },{
                        label:'工行二级商户号',
                        prop:'ghTwoBusinessId',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入工行二级商户号'
                        }]
                    },{
                        label:'招行商户管理员',
                        prop:'zhUserNo',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入招行商户管理员'
                        }]
                    },{
                        label:'招行用户登录密码',
                        prop:'zhLoginPassword',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                            message: '请输入招行用户登录密码'
                        }]
                    },
                    {
                        label:'公司税务号',
                        prop:'saleCompanyNo',
                        display:true,
                        rules:[{
                            required:true,
                            trigger:'blur',
                        }]
                    },
                    {
                        label:'税率类型',
                        prop:'taxType',
                        type:"select",
                        display:true,
                        dicData:Dict.Tax_Type_ENUM,
						rules:[{
						    required:true,
						    trigger:'blur',
						}]
                    }
                ]
            },
        }
    },
    watch: {
        // 'form.payType'(val) {
        //     console.log('val',val)
        //     if(val == 1) { // 招行
        //         console.log('this.newOption.column',this.newOption.column)
        //         this.newOption.column[3].display = true;//门店openId
        //         this.newOption.column[4].display = true;//门店openKey
        //         this.newOption.column[5].display = true;//主管密码
        //         this.newOption.column[6].display = false;//商户公匙路径
        //         this.newOption.column[7].display = false;//商户私匙路径
        //         this.newOption.column[8].display = false;//商户证书密码
        //         this.newOption.column[9].display = false;//商户代号
        //         this.newOption.column[10].display = false;//商户账号
        //         this.newOption.column[11].display = false;//工行分配的AppId
        //         this.newOption.column[12].display = false;//商户私匙
        //         this.newOption.column[13].display = false;//网关公匙
        //     } else if(val == 2) { // 工行
        //         this.newOption.column[3].display = false;//门店openId
        //         this.newOption.column[4].display = false;//门店openKey
        //         this.newOption.column[5].display = false;//主管密码
        //         this.newOption.column[6].display = true;//商户公匙路径
        //         this.newOption.column[7].display = true;//商户私匙路径
        //         this.newOption.column[8].display = true;//商户证书密码
        //         this.newOption.column[9].display = true;//商户代号
        //         this.newOption.column[10].display = true;//商户账号
        //         this.newOption.column[11].display = true;//工行分配的AppId
        //         this.newOption.column[12].display = true;//商户私匙
        //         this.newOption.column[13].display = true;//网关公匙
        //     }
        // },
    },
    methods: {
        //关闭店铺详情弹框
        closeDrawer() {
            this.dialogVisible = false;
            this.$refs.forms.resetForm();
            this.$nextTick(() => {
                this.$refs.forms.clearValidate();
            });
        },

        searchReset() {
            this.query = {};
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = params;
            this.onLoad(this.page, params, done);
        },
        currentChange(currentPage) {
            this.page.currentPage = currentPage;
        },
        sizeChange(pageSize) {
            this.page.pageSize = pageSize;
        },
        refreshChange() {
            this.onLoad(this.page);
        },
        onLoad(page, params = {}, done) {
            this.loading = true;
            getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
                if(done) {
                    done();
                }
                this.loading = false;
                const data = res.data;
                this.page.total = data.count;
                if(data.data.length == 0 && this.page.currentPage > 0) {
                    this.page.currentPage -= 1
                    this.onLoad(this.page);
                }

                this.data = data.data;
            });
        },
        submit(loading,done) {
            this.loading=true;
            console.log(this.form)
            if(this.dialogTitle == "新增") {
                this.form.clientKind = 1;
                add(this.form).then((res) => {
                    this.loading = false;
                    if(res.data.code==200){
                        this.$message({
                            type: "success",
                            message: "操作成功!"
                        });
                    }else{
                        this.$message({
                            type: "error",
                            message:res.data.message
                        });
                    }
                    done()
                    this.closeDrawer()
                    this.onLoad(this.page);
                })
            } else {
                update(this.form).then((res) => {
                    // this.onLoad(this.page);
                    this.loading = false;
                    if(res.data.code==200){
                        this.$message({
                            type: "success",
                            message: "操作成功!"
                        });
                    }else{
                        this.$message({
                            type: "error",
                            message:res.data.message
                        });
                    }
                    done()
                    this.closeDrawer()
                    this.onLoad(this.page);
                    done();
                });
            }
        },
        getEdit(row) {
            this.isView = false;
            this.form = row;
			this.newOption.submitBtn = true
            this.dialogVisible = true;
            this.dialogTitle = "编辑";
        },
        getView(row) {
            this.isView = true;
            this.form = row;
			this.newOption.submitBtn = false
            this.dialogVisible = true;
            this.dialogTitle = "查看";
        },
        handleAdd() {
            this.form.addTime=onlyDateFormat(new Date())
			this.newOption.submitBtn = true
            this.dialogVisible = true;
            this.dialogTitle = "新增";
        },
        getActData(id) {
            let params = {id:id}
            getDetail(params).then((res) => {
                if(res.data.code==200){
                    this.actPlatformData = res.data.data;
                }else{
                    this.$message({
                        type: "error",
                        message:res.data.message
                    });
                }

            });
        },
    }
}
</script>
<style>

</style>
